using Microsoft.Samples.ServiceHosting.StorageClient;
using Microsoft.Samples.ServiceHosting.AspProviders;

namespace KlearStorage
{
    public class FileStorage // TODO: Can provide the same abstraction as in IPersister
    {
        private readonly BlobProvider _provider;

        public FileStorage(string containerName)
        {
            _provider = new BlobProvider(StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration(), containerName);
        }

        public System.IO.MemoryStream GetStream(string blobName)
        {
            BlobProperties properties;
            return _provider.GetBlobContent(blobName, out properties);
        }

        public string GetString(string blobName)
        {
            using (var reader = new System.IO.StreamReader(GetStream(blobName)))
                return reader.ReadToEnd();
        }

        public void Save(string blobName, string content)
        {
            var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content));
            Save(blobName, stream);
        }

        public void Save(string blobName, System.IO.Stream content)
        {
            _provider.UploadStream(blobName, content);
        }
    }
}